package multithread.thredpool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * 1 线程池的超时时间，算进入队列的时间吗？
 * FutureTask-》，包裹了（callable） 实现了RunnableFuture
 * Callable
 *
 */
public class ThredPoolTest {

    static  public class Wrapper{
        private Future future;
        private int taskNum;

        public Wrapper(Future future, int taskNum) {
            this.future = future;
            this.taskNum = taskNum;
        }

        public Future getFuture() {
            return future;
        }

        public void setFuture(Future future) {
            this.future = future;
        }

        public int getTaskNum() {
            return taskNum;
        }

        public void setTaskNum(int taskNum) {
            this.taskNum = taskNum;
        }
    }

    private  static ThreadPoolExecutor executor = null;

    private static void testFeature(){
        executor = new ThreadPoolExecutor(1, 1, 200, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(2));
        List<Wrapper> list = new ArrayList<Wrapper>();
        for(int i=0;i<5;i++){
            MyTask myTask = new MyTask(i);
            Future f = executor.submit(myTask);
            list.add(new Wrapper(f,i));
            //System.out.println("线程池中线程数目："+executor.getPoolSize()+"，队列中等待执行的任务数目："+ executor.getQueue().size()+"，已执行玩别的任务数目："+executor.getCompletedTaskCount());
        }
        for(Wrapper f:list){
            try {
                //不会计算那个任务的进入队列的时间的。get 只是进去判断线程的任务状态
                Object object = f.getFuture().get(600,TimeUnit.MILLISECONDS);
                System.out.println("Future线程:"+f.getTaskNum()+",success：");
            } catch (InterruptedException e) {
                System.out.println("线程:"+f.getTaskNum()+",异常1：");
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                System.out.println("线程:"+f.getTaskNum()+",异常--------------");
                e.printStackTrace();
            }
        }

        while(executor.getActiveCount() >0){

        }
        executor.shutdown();

//        int count = executor.getActiveCount();
//        for(;;){
//            if(count == 0){
//                executor.shutdown();
//                break;
//            }else{
//                count = executor.getActiveCount();
//            }
//            System.out.println("count:"+count);
//        }
    }


    //park unsafe ,队列前面1个执行等待了500
    //Future的超时，不
    public static void main(String[] args) {

        List<String> list1 = new ArrayList<>();
        list1.add("1");
        System.out.println("list:"+list1.toString());


        executor = new ThreadPoolExecutor(1, 1, 200, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(1));
        List<Wrapper> list = new ArrayList<Wrapper>();
        for(int i=0;i<200;i++){
             MyTask myTask = new MyTask(i);
             try{
                 executor.submit(myTask);
                 System.out.println("提交线程"+i+"到里面去");
                 Thread.sleep(1000);
             }catch (Exception e){
                 e.printStackTrace();
             }
        }
    }

    static class MyTask implements Runnable {
        private int taskNum;

        public MyTask(int num) {
            this.taskNum = num;
        }

        @Override
        public void run() {
            System.out.println("task任务:"+taskNum+",开始" +",总共队列数："+executor.getQueue().size()+",activecount："+executor.getActiveCount()+",comleteTaskCount:"+executor.getCompletedTaskCount());
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("task任务:"+taskNum+",执行完毕");
        }
    }
}
